package com.jinlejia.erp.ui.visitor.http

import android.content.Context
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.freedom.yefeng.yfrecyclerview.YfListAdapter
import com.jinlejia.erp.base.MapCallBack
import com.jinlejia.erp.base.VisitorApiConfig
import com.jinlejia.erp.utils.DataFilter
import com.lab.kit.StringKit
import com.zhy.http.okhttp.OkHttpUtils
import okhttp3.Call
import okhttp3.MediaType


class VisitorHttp {
    //region 访客汇总
    fun listCollect(context: Context,keyword:String?,pageIndex:Int,
                    result:(data:ArrayList<Map<String,Any>>,hasNextPage:Boolean)-> Unit,
                    failed:(msg:String)->Unit,adapter:YfListAdapter<*>){
        val json = JSONObject()
        if (!StringKit.isEmpty(keyword)){
            json["keyword"] = keyword
        }
        json["pageIndex"] = pageIndex
        json["pageSize"] = 20
        OkHttpUtils.postString()
            .mediaType(MediaType.parse("application/json; charset=utf-8"))
            .url(VisitorApiConfig.listCollect)
            .content(json.toJSONString())
            .build()
            .execute(object : MapCallBack() {
                override fun onError(call: Call, e: Exception, id: Int) {
                    failed("网络连接失败")
                }

                @Suppress("NAME_SHADOWING", "UNCHECKED_CAST")
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response,adapter)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    val data = json["data"] as JSONArray
                    val hasNextPage = json["nextPageAvailable"] as Boolean
                    val list = ArrayList<Map<String,Any>>()
                    for (d in data){
                        val obj = d as JSONObject
                        val map = obj.toMap<String, Any>()
                        list.add(map)
                    }
                    result(list,hasNextPage)
                }
            })
    }
    //endregion

    //region 访客列表 (最近访客)
    fun recordVisit(context: Context,keyword: String?,pageIndex:Int,
                    result:(data:ArrayList<Map<String,Any>>,hasNextPage:Boolean)-> Unit,
                    failed:(msg:String)->Unit,adapter:YfListAdapter<*>){
        recordVisit(context, keyword, null,pageIndex, result, failed, adapter)
    }
    //endregion
    //region 访客轨迹
    fun recordVisit(context: Context,keyword: String?,guestsCode:String?,pageIndex:Int,
                    result:(data:ArrayList<Map<String,Any>>,hasNextPage:Boolean)-> Unit,
                    failed:(msg:String)->Unit,adapter:YfListAdapter<*>){
        val json = JSONObject()
        if (!StringKit.isEmpty(keyword)){
            json["keyword"] = keyword
        }
        json["pageIndex"] = pageIndex
        json["pageSize"] = 20
        if (!StringKit.isEmpty(guestsCode)){
            json["guestsCode"] = guestsCode
        }
        OkHttpUtils.postString()
            .mediaType(MediaType.parse("application/json; charset=utf-8"))
            .url(VisitorApiConfig.recordVisit)
            .content(json.toJSONString())
            .build()
            .execute(object : MapCallBack() {
                @Suppress("NAME_SHADOWING", "UNCHECKED_CAST")
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response,adapter)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    val data = json["data"] as JSONArray
                    val hasNextPage = json["nextPageAvailable"] as Boolean
                    val list = ArrayList<Map<String,Any>>()
                    for (d in data){
                        val obj = d as JSONObject
                        val map = obj.toMap<String, Any>()
                        list.add(map)
                    }
                    result(list,hasNextPage)
                }

                override fun onError(call: Call, e: java.lang.Exception, id: Int) {
                    failed("网络连接失败")
                }
            })
    }


    //endregion

    //region 访客统计
    fun houseStatis(context: Context,houseCode:String,
                    result:(data:Map<String,Any>)-> Unit,
                    failed:(msg:String)->Unit,adapter:YfListAdapter<*>?){
        OkHttpUtils.get()
            .url(VisitorApiConfig.houseStatis +houseCode)
            .build()
            .execute(object : MapCallBack() {
                @Suppress("NAME_SHADOWING", "UNCHECKED_CAST")
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response,adapter)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    val data = json.toMap<String,Any>()
                    result(data)
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    failed("网络连接失败")
                }
            })
    }
    //endregion

    //region 访客详情-访客列表进入
    fun getCustomerByGuestsCode(context: Context,guestsCode:String,
                                result:(data:JSONObject)-> Unit,
                                failed:(msg:String)->Unit){
        OkHttpUtils.get()
            .url(VisitorApiConfig.getCustomerByGuestsCode +guestsCode)
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    result(json)
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    failed("网络连接失败")
                }
            })
    }
    //endregion

    //region 访客详情 -客源详情需要
    fun getCustomerByCsCode(context: Context,csCode:String,
                            result:(data:JSONObject)-> Unit,
                            failed:(msg:String)->Unit){
        OkHttpUtils.get()
            .url(VisitorApiConfig.getCustomerByCsCode +csCode)
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    result(json)
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    failed("网络连接失败")
                }
            })
    }
    //endregion

    //region 访客详情-聊天进入
    fun getInfoByOpenid(context: Context,openid:String,
                        result:(data:JSONObject)-> Unit,
                        failed:(msg:String)->Unit){
        OkHttpUtils.get()
            .url(VisitorApiConfig.getInfoByOpenid +openid)
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    result(json)
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    failed("网络连接失败")
                }
            })
    }
    //endregion

    //region 访客雷达
    fun visitRadar(context: Context,guestsCode:String,
                   result:(data:JSONObject)-> Unit,
                   failed:(msg:String)->Unit){
        OkHttpUtils.get()
            .url(VisitorApiConfig.visitRadar+guestsCode)
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val filter = DataFilter.filter(context, response)
                    if(!filter){
                        failed(response["message"].toString())
                        return
                    }
                    val json = response["data"] as JSONObject
                    result(json)
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    failed("网络连接失败")
                }
            })
    }

    //endregion

    //region 发送微信订阅
    fun sendWXMessage(openid:String){
        OkHttpUtils.get()
            .url(VisitorApiConfig.SendWXSubscription+openid)
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>?, id: Int) {
                    //不做监听，不添加接口，工具类不会在子线程做请求
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                }
            })
    }
    //endregion

    //region 关联访客
    fun saveCustomer(csCode:String,guestsCode:String,result:(result:Boolean,msg:String?)->Unit){
        val json = JSONObject()
        json["csCode"] = csCode
        json["guestsCode"] = guestsCode
        OkHttpUtils.postString()
            .mediaType(MediaType.parse("application/json; charset=utf-8"))
            .url(VisitorApiConfig.saveCustomer)
            .content(json.toJSONString())
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val code = response["code"].toString()
                    if ("200" == code){
                        result(true,null)
                    }else{
                        val message = response["message"] as String
                        result(false,message)
                    }
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    result(false,"网络失败")
                }
            })
    }
    //endregion

    //region 关联访客-聊天
    fun saveCustomerOpenId(csCode:String,openid:String,result:(result:Boolean,msg:String?)->Unit){
        val json = JSONObject()
        json["csCode"] = csCode
        json["openid"] = openid
        OkHttpUtils.postString()
            .mediaType(MediaType.parse("application/json; charset=utf-8"))
            .url(VisitorApiConfig.saveCustomerOpenId)
            .content(json.toJSONString())
            .build()
            .execute(object : MapCallBack() {
                override fun onResponse(response: MutableMap<String, Any>, id: Int) {
                    val code = response["code"].toString()
                    if ("200" == code){
                        result(true,null)
                    }else{
                        val message = response["message"] as String
                        result(false,message)
                    }
                }

                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    result(false,"网络失败")
                }
            })
    }
    //endregion
}
